package com.mde.mapspathfinder.activ;

import static java.lang.Math.pow;

import java.util.ArrayList;
import java.util.List;

import ru.yandex.yandexmapkit.MapController;
import ru.yandex.yandexmapkit.MapView;
import ru.yandex.yandexmapkit.utils.GeoPoint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.maptasksolver.exceptions.EdgesCountLimitExceeded;
import com.maptasksolver.exceptions.WrongGraphParamsSelected;
import com.maptasksolver.struct.graph.Graph;
import com.maptasksolver.struct.graph.Path;
import com.mde.mapspathfinder.draw.OverlayRect;
import com.mde.yandexmaptrial.R;

public class YandexMapActivity extends Activity
{
	private MapView mapView;
	
	private MapController map;
	
	private GeoPoint ericpol = new GeoPoint(52.089159,23.698327);
	
	private Graph graph;
	
	private int pointsCount = 0;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yandex_layout);
		
		initMap();
				
		//drawPath();
       
    }

	private void drawPath() {
		List<GeoPoint> geoPoints = getGeoPoints();
		
		List<Integer> froms = new ArrayList<Integer>(), 
				wheres = new ArrayList<Integer>();
		
		initVertexes(froms, wheres);
		List<GeoPoint> way = new ArrayList<GeoPoint>();
		try {
			initGraph(geoPoints, froms, wheres);
			Path path = graph.findPathDijkstra(1, 7);
			
			int count = path.getCount();
			
			for (int i = 0; i < count; ++i)
				Log.i("path", Integer.toString(path.getVertex(i)));
			
			for (int i = 0; i < count; ++i)
				way.add(geoPoints.get(path.getVertex(i) - 1));

		} catch (Exception e) {
			Log.i("path", e.getMessage());
		}		
		OverlayRect overlayRect = new OverlayRect(map, way);
		overlayRect.prepareDraw();
        
        map.getOverlayManager().addOverlay(overlayRect);
	}    
    
    private List<GeoPoint> getGeoPoints() {
		List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
		
		// start of good path
		geoPoints.add(new GeoPoint(52.087744,23.703048));
		
		geoPoints.add(new GeoPoint(52.091974,23.700966));
		
		geoPoints.add(new GeoPoint(52.091908,23.700537));
				
		geoPoints.add(new GeoPoint(52.091644,23.700623));
        
		geoPoints.add(new GeoPoint(52.091260,23.698520));
		
		geoPoints.add(new GeoPoint(52.089357,23.699443));
		
		geoPoints.add(new GeoPoint(52.089146,23.698327));
		// endof goog path
		
		geoPoints.add(new GeoPoint(52.090851,23.696417));
		
		geoPoints.add(new GeoPoint(52.087110,23.698091));
		
		geoPoints.add(new GeoPoint(52.087533,23.700451));
		
		geoPoints.add(new GeoPoint(52.087335,23.700966));
		
		pointsCount = 11;
		
		return geoPoints;
	}

	private void initMap() {
		//mapView = (MapView) findViewById(R.id.mapYandex);
		mapView = (MapView)findViewById(R.id.mapYandex);
        map = mapView.getMapController();
		
		mapView.showBuiltInScreenButtons(true);
				
		//map = mapView.getMapController();
		
		map.setZoomCurrent(16);
		
		map.setPositionAnimationTo(ericpol);
		
	}
	
	private void initGraph(List<GeoPoint> points, 
			List<Integer> froms, List<Integer> wheres) 
			throws WrongGraphParamsSelected, EdgesCountLimitExceeded {
		
		int count = points.size();/*
		Log.i("path", "froms size: " + froms.size() + 
				"; wheres size: " + wheres.size() + 
				"; points size: " + points.size() + 
				"; count = " + count);
				*/
		
		graph = new Graph(count, pointsCount);
		
		for (int i = 0; i < count; ++i) {
		
			int from = froms.get(i), where = wheres.get(i);
		
			graph.addEdge(from, where, getDist(points.get(from - 1), points.get(where - 1)), 1);
		}
		//Log.i("path", "Init successful");
		
	}
	
	private double getDist(GeoPoint first, GeoPoint second) {
		return pow(first.getLat() - second.getLat(), 2) + pow(first.getLon() - second.getLon(), 2);
	}

	private void initVertexes(List<Integer> froms, List<Integer> wheres) {
		// good path
		froms.add(1); wheres.add(2);
		
		froms.add(2); wheres.add(3);
		
		froms.add(3); wheres.add(4);
		
		froms.add(4); wheres.add(5);
		
		froms.add(5); wheres.add(6);
		
		froms.add(6); wheres.add(7);
		// endof good path
		
		froms.add(1); wheres.add(11);
		
		froms.add(11); wheres.add(10);
		
		froms.add(10); wheres.add(9);
		
		froms.add(9); wheres.add(8);
		
		froms.add(8); wheres.add(5);		
	}	
	
	@Override
	public void finish() {
	    mapView = null;		
		map = null;		
		graph = null;		
		ericpol = null;   	
		super.finish();
	}
	
}